Operations for sets of long integers or pointers, ReadMe.
Copyright (c) 1996, 1997 by John Montbriand. All Rights Reserved.
Permission hereby granted for public use.
Distribute freely in areas where the laws of copyright apply.
USE AT YOUR OWN RISK.
DO NOT DISTRIBUTE MODIFIED COPIES.
Comments/questions/postcards* to the author at the address:
John Montbriand
P.O. Box. 1133
Saskatoon Saskatchewan Canada
S7K 3N2
or by email at:
tinyjohn@sk.sympatico.ca
*if you mail a postcard, then I will provide you with technical support
regarding questions you may have about this file.
see also: <http://www3.sk.sympatico.ca/tinyjohn>
Contents
About StdSet...
Implementation Notes
Materials
Creating and Disposing of SetHandles
NewSet
BuildSet
DisposeSet
SetHandle Information Access
SetCard
SetEmpty
SetElement
SetMember
SetEqual
SetSubset
Operations on SetHandles
ClearSet
SetCopy
SetAddElt
SetRemoveElt
SetAnd
SetOr
SetXor
SetRemove
Sets and PowerSets
GeneratePowerSet
DisposePowerSet
PowerSetSize
PowerSetElt
Permutations
SetPermute
Sets of Pointers, an example
A Set of What???
Files in this package
Copies for sale!
NO WARRANTY
Bug reports make this a better product!
Further Reference
Other
About StdSet...
SetHandles, and the routines defined in this package that support that data type, define a convenient and simple way to manage sets of 32 bit integers. Although not restricted to thirty-two bit integers, SetHandles are particularly useful when used with this data type as they can be used to manage sets of pointers--with this particular generation of personal computer technology the thirty-two bit integer data type coincides with the size of the pointer data type, used to refer to machine locations.
Implementation Notes
SetHandles are stored as single contiguous blocks of memory. They are appropriate for storage in resource files, or in data files.
These routines can operate on any data type to which the < > >= <= operators apply. You can redefine the type TSETELT in the file StdSet.h to use a different type. Tested with char, short, and long.
These routines are a convenience, not a workhorse. They're not for managing huge sets, and some operations may be slow when they are used on huge sets. If you need to work with massively sized sets, you really should be considering building some tree routines.
Do not lock a SetHandle in memory and expect these routines to work. Always leave SetHandles unlocked. it's best that you treat the SetHandle data type as a transparent type and use the routines provided herein to access it.
If you are using a set handle to store pointers, here's some tips:
- Don't expect any particular ordering. You have no control of the addresses returned to you by the system. You can count on the fact that each pointer will be a unique element in a set.
- Don't store a SetHandle containing pointers in a file or in a resource and expect those pointers to be valid the next time you start the program and read in the SetHandle.
Materials
The SetRecord and SetHandle definition is the copyright property of John Montbriand.
The routines documented herein are the copyright property of John Montbriand. They're fun, easy to use, and practical. I hope all who find them will use them well.
Copyright (c) 1996, 1997 by John Montbriand. All Rights Reserved.
Permission hereby granted for public use.
Distribute freely in areas where the laws of copyright apply.
USE AT YOUR OWN RISK.
DO NOT DISTRIBUTE MODIFIED COPIES.
Creating and Disposing of SetHandles
NewSet
SetHandle NewSet(void);
NewSet creates a new empty set. It returns the value NULL if an error occurs.
BuildSet creates a new set from an unsorted array. It returns the value NULL if an error occurs. count indicates the number of elements stored in the array data. Elements in the array need not be sorted.
DisposeSet reclaims the storage occupied by the set. Note, if you are storing pointers in the set, be sure to dispose of them before disposing of the set.
SetCopy copies the set in src to dst such that the resulting sets are identical. it returns true if the operation was successful. It does not create the set dst, you must call NewSet to create this set before calling SetCopy.
SetAnd returns a new SetHandle containing all of the elements common to both the set a and the set b. Other descriptions of this routine include: 'a and b', or 'set intersection'. returns a new set. If an error occurs, NULL is returned.
-- a is now {1,2,3}, b is {2,3,4}, c is undefi or that subset before calling this routine.
EXAMPLE:
see PowerSetSize.
Permutations
SetPermute
typedef Boolean (*SetPermutation)(TSETELT *elts, long nelts, long param);
OSErr SetPermute(SetHandle a, SetPermutation perm, long param);
SetPermute calls the SetPermutation function, perm, that you define, once for each permutation of the elements in the set a. param is defined for your own use. if the SetPermutation function returns false, SetPermute returns immediately.
EXAMPLE:
Boolean MyPrintPermutationFunction(TSETELT *elts, long nelts, long param) {
The following c program builds a powerset of whatever command line arguments were provided, storing them as pointers in the set. it calls printset for each member of the powerset.
Given the command line arguments, "sets" "are" "handy", the program will print the following output:
subset[0] = {}
subset[1] = {sets}
subset[2] = {are}
subset[3] = {handy}
subset[4] = {sets, are}
subset[5] = {sets, handy}
subset[6] = {are, handy}
subset[7] = {sets, are, handy}
A Set of What???
- a set of windows open in your app,
- a set of files open in your app,
- a set of volumes mounted on the computer,
- a set of strings,
- a set of dialogs in your app,
- a set of fonts usable in a particular document,
and so on, and so on...
Files in this package
StdSet.c -- source code for the StdSet routines' implementation
StdSet.h -- header file providing interfaces to the StdSet routines
ReadMe -- this file
:Libraries: -- precompiled libraries, ready to link.
StdSet.o -- 68K version of the StdSet library
StdSet.xcoff -- PowerPC version of the StdSet library
Copies for sale!
These libraries are provided for free and you may use them in any program you make; however, if you would like to purchase a copy of these libraries and have a legal paper trail establishing your right to use them, then send along a cheque or a money order in the amount of $25.00 for the purchase of one copy. I'll send you a receipt.
NO WARRANTY
No warranties are made regarding these files. John Montbriand disclaims all warranties regarding these files, either express or implied, including but not limited to implied warranties of merchantability and fitness for any particular purpose. These files are provided "AS IS" without any warranty of any kind. Use them at your own risk. Copies of these files are not for sale in areas where the law does not allow exclusion of implied warranties.
Bug reports make this a better product!
As in all of my products, I advertise a $10.00 finder's fee for bug reports that lead to corrections. If you find a problem here, report it! it could be worth your while....
Further Reference
Inside Macintosh: Memory by Apple Computer, Inc. Addison-Wesley.
A discussion of memory management, handles, what they are, and how to use them.
The C Programming Language 2nd edition by Brian W. Kernighan and Dennis M. Ritchie. Prentice Hall.